// pages/shezhi/shezhi.js
const app = getApp()

var util = require('../../js/util.js');
var api = require('../../js/api.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: '',
        //滑块使用
        isC: true, //默认摄氏度
        isF: false,
        isAudio: true, //声音，默认开

        isFucha: false, //复查提醒，默认关闭
        time: '06:00',


    },

    //切换华氏度
    qhC(e) {
        var that = this
        that.checkconn(function (res) {
            if (res) {
                console.log('====>>切换C' + e.detail.value)
                that.setData({
                    isC: e.detail.value,
                    isF: !e.detail.value,
                })
                that.control()
            }
        })

    },
    qhF(e) {
        var that = this
        that.checkconn(function (res) {
            if (res) {
                console.log('====>>切换F' + e.detail.value)
                that.setData({
                    isC: !e.detail.value,
                    isF: e.detail.value,
                })
                that.control()
            }
        })

    },


    //开关声音
    qhAudio(e) {
        var that = this
        that.checkconn(function (res) {
            if (res) {
                console.log('====>>切换A' + e.detail.value)
                that.setData({
                    isAudio: e.detail.value
                })
                that.control()
            }
        })

    },

    //切换复查
    qhFucha(e) {

        this.setData({
            isFucha: e.detail.value
        })
        if (!e.detail.value) {
            api.delWxRemind(function (res) {
                api.findWxRemind()
            })
        }
    },

    //时间选择取消
    bindTimecancel: function () {
        var that = this
        that.setData({
            isFucha: false,

        })
    },


    //时间选择确认
    bindTimeChange: function (e) {
        console.log(e)
        var that = this
        console.log('picker发送选择改变，携带值为', e.detail.value)
        that.setData({
            time: e.detail.value

        })

        wx.showModal({
            title: '订阅授权',
            // content: '请勾选底部 总是保持以上选择，不再询问',

            mask: true,
            duration: 3000,
            success: function (res) {
                if (res.confirm) {

                    requestMsg().then(res => {

                        if ('ok' === res) {
                            console.log('最终订阅成功')

                            //TODO: 将订阅时间发给服务器

                            if (app.globalData.wxRemind == null) {
                                //add
                                let wxRemind = {
                                    time: '2000-01-01 ' + that.data.time + ':00'
                                }
                                api.addWxRemind(wxRemind, function (res) {
                                    api.findWxRemind()
                                })

                            } else {
                                //put
                                let wxRemind = {
                                    time: '2000-01-01 ' + that.data.time + ':00',
                                    wxRemindId: app.globalData.wxRemind.wxRemindId,

                                }
                                api.putWxRemind(wxRemind, function (res) {
                                    api.findWxRemind()
                                })
                            }


                        } else {
                            console.log('最终订阅失败')
                            that.setData({
                                isFucha: false,

                            })
                        }
                    })

                } else if (res.cancel) {
                    console.log('用户点击取消')
                    that.setData({
                        isFucha: false,

                    })
                }

                console.log('==确认订阅授权')

            }


        })


    },


    onTabItemTap: function () {
        if (api.checkToken()) {
            var that = this
            let rtime = '06:00'
            if (app.globalData.wxRemind != null) {
                rtime = (app.globalData.wxRemind.time).substr(11)
                rtime = rtime.substr(0, 5)
            }
            console.log('====>>rtime\n' + rtime)

            that.setData({
                userInfo: app.globalData.userInfo,
                isC: app.globalData.cels,
                isF: !app.globalData.cels,
                isAudio: app.globalData.beep,
                isFucha: app.globalData.wxRemind == null ? false : true,
                time: rtime,


            })
        }

    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (api.checkToken() && !app.globalData.equconn0) {
            if (app.globalData.equ.mac != null && app.globalData.equ.mac != '') {
                wx.reLaunch({
                    url: '/pages/jiance/jiance'
                })
            } else {
                wx.reLaunch({
                    url: '/pages/shebei/shebei'
                })
            }
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    checkconn: function (callback) {
        callback && typeof callback === 'function' ? callback(app.globalData.equconn0) : null;
    },

    control: function () {
        var that = this
        let str1 = 'IR8A+A2,beep='
        let str2 = 'IR8A+A3,cels='

        if (that.data.isAudio) {
            app.globalData.beep = true
            str1 += 'T'
        } else {
            app.globalData.beep = false
            str1 += 'F'
        }


        if (that.data.isC) {
            app.globalData.cels = true
            str2 += 'T'
        } else {
            app.globalData.cels = false
            str2 += 'F'
        }


        util.sendMsg(str1)
        util.sendMsg(str2)

        wx.showToast({
            title: '正在设置',
            icon: 'loading',
            mask: true,
            duration: 2000,


        })
    }


})

// login.js
function requestMsg() {
    return new Promise((resolve, reject) => {
        wx.requestSubscribeMessage({
            tmplIds: ["ut5p5BnDvZ9swHk5puHO3GbMkmYz7geWAA5r1svUx1Y"],
            success: (res) => {
                console.log('===>>点击订阅')
                console.log(res)


                if (res['ut5p5BnDvZ9swHk5puHO3GbMkmYz7geWAA5r1svUx1Y'] === 'accept') {
                    console.log('====>>订阅成功')
                    wx.showToast({
                        title: '订阅OK！',
                        duration: 1000,
                        success(data) {
                            //成功
                            resolve('ok')
                        }
                    })
                } else {
                    resolve('fail')
                }
            },
            fail(err) {
                console.log('====>>订阅失败')
                //失败
                console.error(err);
                reject('err')
            }
        })
    })
}
